package dkbta.util;

/**
 * A generic pair
 * 
 * @param <F> Any type
 * @param <S> Any type
 * @author Uri Kanonov
 */
public class Pair<F, S> {
	/** The first element of the pair */
	private final F _first;

	/** The second element of the pair */
	private final S _second;

	/**
	 * Packages two given elements in a pair
	 * 
	 * @param first The first element of the pair
	 * @param second The second element of the pair
	 */
	public Pair(F first, S second){
		_first = first;
		_second = second;
	}

	/**
	 * Returns the first element of the pair
	 * 
	 * @return The first element of the pair
	 */
	public F getFirst(){
		return _first;
	}

	/**
	 * Returns the second element of the pair
	 * 
	 * @return The second element of the pair
	 */
	public S getSecond(){
		return _second;
	}

	/**
	 * A pair is only equal to other pairs whose first element is equal to this pair's
	 * first element and same goes for the second element
	 * 
	 * @param other The object to be compared to
	 * @return Whether this pair is equal to the other object
	 */
	@Override
	@SuppressWarnings("unchecked")
	public boolean equals(Object other){
		if (other instanceof Pair){
			Pair o = (Pair)other;
			return o._first.equals(_first) && o._second.equals(_second);
		}else{
			return false;
		}
	}

	/**
	 * Calculates the hashcode for this pair based on it's first and second elements
	 * 
	 * @return The hashcode for this pair
	 */
	@Override
	public int hashCode(){
		return toString().hashCode();
	}

	/**
	 * Returns a string representation of this pair
	 * 
	 * @return A string representation of this pair
	 */
	@Override
	public String toString(){
		return "(" + _first + ", " + _second + ")";
	}

}
